﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AlarmManagePlatform.Model;

namespace AlarmManagePlatform.UC
{
    public partial class CheckedInstrumentDetail : UserControl
    {
        Forms.ImageBrowser ShowValueImageInfo;
        public CheckedInstrumentDetail()
        {
            InitializeComponent();

            Static.Methods.CreateDGVColumns(ref dgvCheckedInstrumentDetail, Model.SystemEnum.DGVType.CheckedInstrumentDetail);
            dgvCheckedInstrumentDetail.CellDoubleClick += DgvCheckedInstrumentDetail_CellDoubleClick;
            dgvCheckedInstrumentDetail.Columns["Selected"].Visible = false;
            dgvCheckedInstrumentDetail.Columns["ParaName"].Visible = false;
        }

        private void DgvCheckedInstrumentDetail_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;

            Bitmap _BitMap = null; System.IO.Stream _Stream = null;

            Model.JSON.JSONDemoMediaInfo _JSONSDemoMediaInfo;

            string _VoiceFile = string.Empty;
            string _ErrorInfo = string.Empty;
            try
            {
                Model.CheckDataInfo _CheckDataInfo = (Model.CheckDataInfo)dgvCheckedInstrumentDetail.Rows[e.RowIndex].Tag;
                
                string[] _Params = new string[2];
                _Params[0] = Static.ParaInfo.CurrentUser.tonken;
                _Params[1] = "{\"dataId\":" + _CheckDataInfo.id.ToString() + "}";

                object result = Relations.Common.WebServiceHelper.InvokeWebService(Static.ParaInfo.webServiceURL, "getDataMediaInfo", _Params);

                if (Static.Methods.webServiceCheckResultIsRight(result.ToString(), out _ErrorInfo))
                {
                    _JSONSDemoMediaInfo = Static.Methods.JOSNConvert<Model.JSON.JSONDemoMediaInfo>(result.ToString());

                    if (string.IsNullOrEmpty(_JSONSDemoMediaInfo.mediaInfo.imageFile) && string.IsNullOrEmpty(_JSONSDemoMediaInfo.mediaInfo.voiceFile))
                    {
                        MessageBox.Show(this, "无相关媒体信息！" , "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    if (!string.IsNullOrEmpty(_JSONSDemoMediaInfo.mediaInfo.imageFile))
                        _BitMap = Relations.Common.Methods.Base64StringToImage(_JSONSDemoMediaInfo.mediaInfo.imageFile);
                    else
                        _BitMap = null;

                    if (!string.IsNullOrEmpty(_JSONSDemoMediaInfo.mediaInfo.voiceFile))
                    {
                        //Relations.Common.Methods.Base64StringToFile(_JSONSDemoMediaInfo.mediaInfo.voiceFile, Static.ParaInfo.VoiceFilePath, true);
                        //_VoiceFile = Static.ParaInfo.VoiceFilePath;

                        byte[] bytes = Convert.FromBase64String(_JSONSDemoMediaInfo.mediaInfo.voiceFile);
                        _Stream = new System.IO.MemoryStream(bytes);
                    }
                    else
                        _VoiceFile = string.Empty;

                    Forms.IVShowForm iVShowForm = new Forms.IVShowForm(_CheckDataInfo.ParaValue);
                    iVShowForm.Stream = _Stream;
                    iVShowForm.Bitmap = _BitMap;

                    iVShowForm.ShowDialog();

                }
                else
                {
                    MessageBox.Show(this, "获取图片失败，错误信息：" + _ErrorInfo, "异常信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "显示图片失败，错误信息：" + ex.Message, "异常信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void _ShowValueImageInfo_Event_UpdateImageResult(CheckDataInfo obj)
        {
        }

        internal void ClealAll()
        {
            dgvCheckedInstrumentDetail.Rows.Clear();
        }

        internal void ShowCheckedInstrumentDetail(string instrumentId, int hIS_IndexId, string his_CreateTime, float testValue)
        {
            List<Model.CheckDataInfo> _CheckDataInfoList = new DataAccess.tb_AlarminstrumentHisDeailData().GetCheckDataInfo(instrumentId, hIS_IndexId, his_CreateTime);
            dgvCheckedInstrumentDetail.Rows.Clear();
            foreach (Model.CheckDataInfo _CheckDataInfo in _CheckDataInfoList)
            {
                dgvCheckedInstrumentDetail.Rows.Add();
                _CheckDataInfo.TestValue = testValue;
                dgvCheckedInstrumentDetail.Rows[dgvCheckedInstrumentDetail.Rows.Count - 1].Tag = _CheckDataInfo;
                dgvCheckedInstrumentDetail.Rows[dgvCheckedInstrumentDetail.Rows.Count - 1].Cells["ParaName"].Value = _CheckDataInfo.tb_ParaInfo.Desc;
                dgvCheckedInstrumentDetail.Rows[dgvCheckedInstrumentDetail.Rows.Count - 1].Cells["ParaValue"].Value = _CheckDataInfo.ParaValue;
                dgvCheckedInstrumentDetail.Rows[dgvCheckedInstrumentDetail.Rows.Count - 1].Cells["CreateDate"].Value = _CheckDataInfo.CreateDate;
            }
        }
    }
}
